import request from "@/uni-sub-base/plugins/request"
import configs from "@/config/config.js"
import storage from "@/uni-sub-base/utils/storage.js"
import { gotoLogin, gotoLoginUni } from "@/uni-sub-base/utils/utils.js"
import store from "@/store/index.js"
import cookies from "@/uni-sub-base/module/vue-cookies/vue-cookies.js"
import { setCallbackUrl } from '@/utils/utils.js'

let headerConfig = {
    // 'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
    // 'Authorization': "Bearer " + token,
}
// 设置全局配置
request.prototype.setConfig({
    ...headerConfig,
    // url: configs.baseUrl, // 基地址
})

// 全局拦截器
request.prototype.addGlobalInterce({
    // 请求拦截器 (例如配置token)
    // return false或者不return值, 都不会发送请求
    request(config) {
        let token = store.state.token
        //先取store 的值
        if (!token || token == null || token == "") {
            //没有的话取token
            token = storage.getCache("token")
        }
        if (!token || token == null || token == "") {
            //没有的话取webInfo
            let webInfo = storage.getCache("webInfo")
            if (webInfo != null) {
                token = webInfo.Token
            }
        }
        if (token && token != "") {
            //有token
            config.header = {
                Authorization: "Bearer " + token,
            }
        }
        config.header["content-type"] = "application/json;charset=utf-8"
        return config
    },

    // 响应拦截器 (例如根据状态码拦截数据)
    // return false或者不return值 则都不会返回值
    // return Promise.reject('xxxxx')，主动抛出错误
    response(res) {
        let message = ""
        if (res.statusCode == 200) {
            if (res.data.result != 1) {
                message = res.data.msg
            }
        }

        if (res.statusCode == 400) {
            message = "请求错误"
        }

        if (res.statusCode == 401) {
            message = "未授权，请登录"
        }

        if (res.statusCode == 403) {
            message = "拒绝访问"
        }

        if (res.statusCode == 404) {
            message = `请求地址出错`
        }

        if (res.statusCode == 408) {
            message = "请求超时"
        }

        if (res.statusCode == 500) {
            // message = "服务器内部错误"
        }

        if (res.statusCode == 501) {
            message = "服务未实现"
        }

        if (res.statusCode == 502) {
            message = "网关错误"
        }

        if (res.statusCode == 503) {
            message = "服务不可用"
        }

        if (res.statusCode == 504) {
            message = "网关超时"
        }

        if (res.statusCode == 505) {
            message = "HTTP版本不受支持"
        }

        if (message) {
            uni.showToast({
                title: message,
                icon: "none",
                duration: 2000,
            })
            return false
        }
        try {
            if (res.data.State == 2 && res.data.Msg == "登录异常") {
                setCallbackUrl()
                gotoLoginUni()
                return
            }
            if (!!res && !!res.data && !!res.data.State && res.data.State == 2 && configs.isOldH5) {
                //需要旧的h5页面登录
                // gotoLogin()
                gotoLoginUni()
                return
            } else if (res && res.data && res.data.State == 5) {
                uni.showToast({
                    title: res.data.Msg,
                    icon: "none",
                    duration: 2000,
                })

                //强制退出登录
                setTimeout((e) => {
                    gotoLoginUni()
                }, 800)
            }
        } catch (e) {
            uni.showToast({
                title: e,
                icon: "none",
                duration: 2000,
            })
        }
        // 停止发送请求 request.stop()
        if (JSON.stringify(res) === '{"errMsg":"request:fail abort"}') {
            // do something
            // return Promise.reject('xxxxxxxxx');
            return false
        }

        if (!!res && !!res.header && res.header.Authorization) {
            uni.setStorageSync(token, res.header.Authorization)
        }

        // return Promise.reject(res)
        return res.data
    },
})

export const http = new request()
